﻿''' <summary>
''' Provides a GameObject whose image is a rendered GDI Path.  A GdiShape can be easily set to a regular polygon, or to
''' any polygon defined by an array of PointF structures.
''' </summary>
''' <remarks></remarks>
Public Class GdiShape
    Inherits GameObject

    Public Property AutoPolygon As Boolean = True
    Public Property HatchBrushStyle As System.Drawing.Drawing2D.HatchStyle = -1
    Public Property LinearGradientStyle As System.Drawing.Drawing2D.LinearGradientMode = -1
    Public Property FillColorOne As Color = Color.White
    Public Property FillColorTwo As Color = Color.Black
    Public Property StrokeWidth As Single = 0.0F
    Public Property StrokeColor As Color = Color.Black

    Private _PathPoints() As PointF
    Public Property PathPoints() As PointF()
        Get
            Return _PathPoints
        End Get
        Set(value As PointF())
            _PathPoints = value
            If Not AutoPolygon Then ResetPathPoints()
        End Set
    End Property

    Private _Radius As Single = 12.0F
    Public Property Radius As Single
        Get
            Return _Radius
        End Get
        Set(value As Single)
            If value > 0 Then
                _Radius = value
                If AutoPolygon Then ResetPolygon()
            End If
        End Set
    End Property

    Private _Sides As Integer = 3
    Public Property Sides As Integer
        Get
            Return _Sides
        End Get
        Set(value As Integer)
            If value > 2 Then
                _Sides = value
                If AutoPolygon Then ResetPolygon()
            End If
        End Set
    End Property

    Private _LastPosition As Vector2
    Private _Path As New System.Drawing.Drawing2D.GraphicsPath

    Public Sub New()
        MyBase.New()
        IsCollider = False
    End Sub

    Protected Friend Overrides Sub OnLoad(e As GameEngineEventArgs)
        MyBase.OnLoad(e)
        ResetPolygon()
    End Sub

    Protected Friend Overrides Sub OnDraw(e As DrawEventArgs)
        If HatchBrushStyle > -1 Then
            Dim fillBrush As New System.Drawing.Drawing2D.HatchBrush(HatchBrushStyle, FillColorOne, FillColorTwo)
            e.PaintEventArgs.Graphics.FillPath(fillBrush, _Path)
            fillBrush.Dispose()
        End If
        If LinearGradientStyle > -1 Then
            Dim gradientBounds As RectangleF = _Path.GetBounds
            gradientBounds.X = 0
            gradientBounds.Y = 0
            Dim fillBrush As New System.Drawing.Drawing2D.LinearGradientBrush(gradientBounds, FillColorOne, FillColorTwo, LinearGradientStyle)
            e.PaintEventArgs.Graphics.FillPath(fillBrush, _Path)
            fillBrush.Dispose()
        End If
        If StrokeWidth > 0 Then
            Dim strokePen As New Pen(StrokeColor, StrokeWidth)
            e.PaintEventArgs.Graphics.DrawPath(strokePen, _Path)
        End If
        MyBase.OnDraw(e)
    End Sub

    Protected Friend Overrides Sub OnUpdate(e As GameEngineEventArgs)
        If Not _LastPosition = Position Then
            If AutoPolygon Then
                ResetPolygon()
            Else
                ResetPathPoints()
            End If
            _LastPosition = Position
        End If
        MyBase.OnUpdate(e)
    End Sub

    Public Sub ResetPathPoints()
        _Path.Reset()
        _Path.AddLines(_PathPoints)
        _Path.CloseFigure()
        Dim bounds As RectangleF = _Path.GetBounds
        Dim translation As New System.Drawing.Drawing2D.Matrix
        translation.Translate(Position.X - (bounds.Width / 2), Position.Y - (bounds.Height / 2))
        _Path.Transform(translation)
    End Sub

    Public Sub ResetPolygon()
        _Path.Reset()
        _Path.AddLines(Geometry.ConvertArrayVector2ToPointF(Geometry.GetPolygonPoints(_Radius, _Sides, Position)))
        _Path.CloseFigure()
    End Sub
End Class
